import { getBankList,confirmSign,readySign,getBankAgree } from "../../../../api/bank";
const app = getApp();
Page({
  data: {
    title: 'picker',
    array: ['请选择卡类型'],
    index: 0,
    bank_id: '',
    bankList: {},
    bank_id1: 0,
    userInputCardNum: '', // 银行卡账号
    phoneNum: '', //预留手机号
    cardNum: '',
    uniqueCode: '',
    phoneCode: '',
    codeName: '获取验证码',
    totalTime1: 60,
    isSend1: false, //禁用
    timer: '', //定时器
    argeeList: '',
    radioValue: false
  },
  onLoad() {
    getBankList().then(res => {
      this.setData({
        array:this.data.array.concat(res),
        bankList:res,
      });
      // console.log(this.data.array);
    })
  },
  radioCheck(e) {
    console.log(this.data.radioValue);
    this.setData({radioValue:!this.data.radioValue});
  },
  handleBlur(e) {
    this.setData({phoneNum:e.detail.value});
  },
  handlePhoneCode(e){
    this.setData({phoneCode:e.detail.value});
  },
  handleClick() {
    this.setData({showPicker:true});
  },
  bindPickerChange(e) {
    this.setData({index:e.detail.value});
    for (let item in this.data.bankList) {
      if (this.data.array[e.detail.value] == this.data.bankList[item]) {
        this.setData({bank_id:item});
      }
    }
   this.getArrList()
  },
  getArrList() {
  	let str = ''
  	getBankAgree(this.data.bank_id).then(res => {
  		this.setData({argeeList: res});
  	})
  },

  toArgee1(e) {
    my.navigateTo({
      url: '/pagesA/pages/bankCard/bankAgree/bankAgree?bank_id='+this.data.bank_id
    })
  },

  toArgee(e) {
    my.navigateTo({
      url: '/pagesA/pages/bankCard/bankAgree/bankAgree?bank_id=300'
    })
  },
  bankcardInput(e) {
    if (this.index == 0) {
      my.showToast({
        title: "请先选择卡类型",
        icon: 'error'
      })
    } else {
      // var card = e.detail.value;
      //输入时每四位一个空格
      // var card = card.replace(/\s/g, '').replace(/[^\d]/g,
      //   '').replace(/(\d{4})(?=\d)/g, '$1 ');
      // // 将处理后的值赋予到输入框
      // this.setData({userInputCardNum:card});
      // // 输入的卡号长度
      // var len = card.length
      // //判断用户是输入还是回删
      // if (len > this.cardlen) {
      //   //用户输入
      //   if ((len + 1) % 5 == 0) {
      //     card = card + ' '
      //     console.log(card);
      //   }
      // } else {
      //   //用户回删
      //   card = card.replace(/(^\s*)|(\s*$)/g, "")
      // }
      // // 将处理后的值赋予到输入框
      // this.setData({userInputCardNum:card});
      // this.cardlen = len
      // //将每次用户输入的卡号长度赋予到长度中转站
      // // 格式化字符串的空格
      // let cardNum = this.data.userInputCardNum.replace(/\s*/g, "");
      this.setData({cardNum:e.detail.value});
    }
  },
  handleCode() {
    let data = {
      cardNo: this.data.cardNum,
      mobileNo: this.data.phoneNum,
      bank_id: this.data.array[this.data.index]
    }
    if (!data.bank_id) {
      console.log(data.bank_id);
      my.showToast({
        content: '请选择卡类型',
        type: 'fail',
        duration: 1000
      })
      return;
    }

    if (!data.cardNo) {
      my.showToast({
        content: '请输入卡号',
        type: 'fail',
        duration: 1000
      })
      return;
    }


    const phoneReg = /^1[3456789]\d{9}$/
      if (!phoneReg.test(data.mobileNo)) {
        my.showToast({
          content: '手机号码格式有误',
          type: 'fail',
          duration: 1000
        })
        return;
      }

    readySign(data).then(res => {
      this.setData({uniqueCode:res}); 
    })
    this.setData({isSend1:true}); 
    this.setData({codeName:this.data.totalTime1 + 's后重新发送'}); 
 
    this.data.timer1 = setInterval(() => {
      this.data.totalTime1--
      this.setData({codeName:this.data.totalTime1 + 's后重新发送'}); 

      if (this.data.totalTime1 < 0) {
        clearInterval(this.data.timer1)
        this.setData({codeName:'重新发送验证码',totalTime1:60,isSend1:false}); 
      }
    }, 1000)
  },
  handleFished() {  
    let data = {
      cardNo: this.data.cardNum,
      mobileNo: this.data.phoneNum,
      bank_id: this.data.array[this.data.index]
    }
    const phoneReg = /^1[3456789]\d{9}$/
    if (!phoneReg.test(data.mobileNo)) {
      my.showToast({
        content: '手机号码格式有误',
        type: 'fail',
        duration: 1000
      })
      return;
    }

    if (this.data.radioValue == true) {
      let data = {
        verificationCode: this.data.phoneCode,
        msgId: this.data.uniqueCode
      }
      confirmSign(data).then(res => {
        my.showToast({
          type: 'success',
          content: '绑卡成功',
          duration: 2000
        })
        my.navigateBack();
      })
    } else {
      my.showToast({
        content: '请先阅读协议',
        type: 'fail',
        duration: 1000
      })
      return
    }
  },
});
